from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.ext.declarative import declarative_base

from src.config import settings


class DataBase:
    def __init__(self, db_name=settings.db_path):
        self.Models = declarative_base()
        self.Engine = create_engine(f'sqlite:///{db_name}')
        self.SessionFactory = sessionmaker(bind=self.Engine)
        self.Session = scoped_session(self.SessionFactory)

    def init_db(self):
        self.Models.metadata.create_all(self.Engine)

    def clear_db(self):
        self.Models.metadata.drop_all(self.Engine)

    def __del__(self):
        self.Session.remove()


db = DataBase()
